class Token {
    constructor(store, options) {
        this.store = store;
        this.options = options;
        this.install();
        return this;
    }
    install() {
        this.store.registerModule(["token"], this.options.data());
        let obj = this.options.mixin();
        for (let n in obj) {
            if (n !== "data") this[n] = obj[n];
        }
        return this;
    }
    uninstall() {
        this.store.unregisterModule(["token"]);
        return this;
    }
    has() {
        let token = localStorage.getItem("token");
        let result =
            (token && token !== "") ||
            (this.store.state.user.token && this.store.state.user.token !== "") ?
            true :
            false;
        return result;
    }
}

export const token = function(Vue, store) {
    return new Promise((resolve, reject) => {
        try {
            Vue.prototype.$token = Vue.$token = new Token(store, {
                data() {
                    return {
                        state: {
                            user: {
                                token: null,
                            },
                        },
                        actions: {
                            getSMSCode(store, json) {
                                return new Promise((resolve, reject) => {
                                    store
                                        .dispatch("requestData", {
                                            path: "/login/sms",
                                            data: {...json },
                                        })
                                        .then((v) => {
                                            resolve(v);
                                        })
                                        .catch((v) => {
                                            reject(v);
                                        });
                                });
                            },
                            logIn(store, json) {
                                return new Promise((resolve, reject) => {
                                    store
                                        .dispatch("requestData", { path: "/login/in", data: json })
                                        .then((v) => {
                                            store.dispatch("saveToken", { token: v.data.token });
                                            resolve(v);
                                        })
                                        .catch((v) => {
                                            reject(v);
                                        });
                                });
                            },
                            logOut(store, json) {
                                return new Promise((resolve, reject) => {
                                    store
                                        .dispatch("requestData", {
                                            path: "/login/quit",
                                            data: json,
                                        })
                                        .then((v) => {
                                            store.dispatch("deleteToken");
                                            store.dispatch("deleteUserInfo");
                                            resolve(v);
                                        })
                                        .catch((v) => {
                                            reject(v);
                                        });
                                });
                            },
                            saveToken(store, json) {
                                return new Promise((resolve, reject) => {
                                    try {
                                        store.commit("saveToken", json);
                                        resolve();
                                    } catch (e) {
                                        reject(e);
                                    }
                                });
                            },
                            deleteToken(store) {
                                return new Promise((resolve, reject) => {
                                    try {
                                        store.commit("deleteToken");
                                        resolve();
                                    } catch (e) {
                                        reject(e);
                                    }
                                });
                            },
                        },
                        mutations: {
                            saveToken(state, json) {
                                state.user.token = json.token;
                                localStorage.setItem("token", json.token);
                            },
                            deleteToken(state) {
                                state.user.token = null;
                                localStorage.removeItem("token");
                            },
                        },
                    };
                },
                mixin() {
                    return {
                        login(json) {
                            return this.store.dispatch("logIn", json);
                        },
                        logout(json) {
                            return this.store.dispatch("logOut", json);
                        },
                        saveToken(json) {
                            return this.store.dispatch("saveToken", json);
                        },
                        deleteToken(json) {
                            return this.store.dispatch("deleteToken", json);
                        },
                        getSMSCode(json) {
                            return this.store.dispatch("getSMSCode", json);
                        },
                    };
                },
            });
            resolve(Vue);
        } catch (e) {
            reject(e);
        }
    });
};